////////dev_ac_meter//////////
/*-------------------------------------------------------------------------------------------------------

{"head":{"gatewayId":"gw_1C3283005078","msgId":12,"timeStamp":0,"cmd":"readAttr"},"data":{"privAddr":5,"channel":1,"nodeType":1,"mask":"01","classMask":"11"}}
Tcp_ch1_TxData copy done
gw_ktj socket send done
11:41:16- ETH_1 local_ktj_js Rx: len=1467
{"head":{"gatewayId":"gw_1C3283005078","replaceId":"","msgId":12,"timeStamp":1736912476,"response":"readAttr"},
"data":{"nodeType":1,"channel":1,"privAddr":5,"mac":"FFFFFFFFFFFF","name":"eleMeter_1_5","pid":"4005",
"totalFrame":1,"currFrame":1,"totalAttr":15,"itemCnt":15},"attrLst":[
{"name":"rptVoltA","lable":"a相电压上报","class":17,"dpid":1045,"val":2228,"type":0},
{"name":"rptCurrA","lable":"a相电流上报","class":17,"dpid":1027,"val":0,"type":0},
{"name":"rptActPwrA","lable":"a相功率上报","class":17,"dpid":1036,"val":0,"type":0},
{"name":"rptPfA","lable":"a相功率因数上报","class":17,"dpid":1039,"val":100,"type":0},
{"name":"rptFreq","lable":"频率上报","class":17,"dpid":1032,"val":500,"type":0},
{"name":"di1","lable":"di1","class":17,"dpid":32852,"val":0,"type":0},
{"name":"di2","lable":"di2","class":17,"dpid":32853,"val":0,"type":0},
{"name":"di3","lable":"di3","class":17,"dpid":32854,"val":0,"type":0},
{"name":"di4","lable":"di4","class":17,"dpid":32855,"val":0,"type":0},
{"name":"do1","lable":"do1","class":17,"dpid":32856,"val":0,"type":0},
{"name":"do2","lable":"do2","class":17,"dpid":32857,"val":0,"type":0},
{"name":"di","lable":"di","class":17,"dpid":1057,"val":0,"type":0},
{"name":"do","lable":"do","class":17,"dpid":1058,"val":0,"type":0},
{"name":"rptActEngiT","lable":"有功电能上报","class":17,"dpid":1035,"val":0,"type":0},
{"name":"rptReactEngiT","lable":"无功电能上报","class":17,"dpid":1056,"val":0,"type":0}]}




today at 14:39:39  [2025-02-07 14:39:39.628]	INFO	RECV, DATA= 
{"data":{"deviceName":"eleMeter_1_5","mac":"FFFFFFFFFFFF","nodeType":1,"channel":1,"privAddr":5},
"attrLst":[{"class":17,"dpid":32857,"lable":"do2","name":"do2","type":0,"val":"0","order":1}],
"head":{"gatewayId":"gw_CC0500CFB180","msgId":0,"timeStamp":0,"cmd":"setAttr","appuserId":27,"replaceId":"gw_1738909887431",

"indicate":"setAttr","isreplace":true,"replace_content":"{\"data\":{\"deviceName\":\"eleMeter_1_5\",\"mac\":\"FFFFFFFFFFFF\",
\"nodeType\":1,\"channel\":1,\"privAddr\":5},\"attrLst\":[{\"class\":17,\"dpid\":32857,\"lable\":\"do2\",\"name\":\"do2\",\"type\":0,\"val\":\"0\",\"order\":1}],
\"head\":{\"gatewayId\":\"gw_CC0500CFB180\",\"msgId\":0,\"timeStamp\":0,\"cmd\":\"setAttr\",\"replaceId\":\"gw_1738909887431\"}}\n"}}

today at 14:39:39  [2025-02-07 14:39:39.716]	INFO	RECV, DATA= 
{"head":{"gatewayId":"gw_CC0500CFB180","replaceId":"gw_1738909887431","msgId":0,"timeStamp":1738910375,"response":"setAttr"},
"data":{"pid":"4005","mac":"FFFFFFFFFFFF","deviceName":"eleMeter_1_5","attr":{}},"errCode":0}


Tcp_Tx_ch=1, local_ktj_js
{"head":{"gatewayId":"gw_CC0500CFB180","msgId":44,"timeStamp":0,"cmd":"setAttr"},
"data":{"deviceName":"eleMeter_1_5","mac":"FFFFFFFFFFFF","nodeType":1,"channel":1,"privAddr":5},
"attrLst":[{"name":"do1","val":"1"},{"name":"do2","val":"1"}]}


-------------------------------------------------------------------------------------------------------*/

//#include "dev_ac_meter.h"


#ifndef __DEV_AC_METER_H__
#define __DEV_AC_METER_H__

#include "dev_module.h"
#include "iot_message.pb.h"
#include <stdint.h>
#include "iot_node.h"




#define  ac_meter_max_scale_0_01mm   (100000U)    //1000mm===1000x100 (0.01mm)
#define  ac_meter_max_dev_addr        (245U)      // 烟雾设备地址	R/W	否	配置参数	uint8_t	0~255
#define  ac_meter_max_channel          (31U)      // 烟雾设备通道编号	R/W	否	配置参数	uint8_t	0~31




/*-----------------------------------------------------------------------------------------------							
0x0801	软件版本	RO	否	软件版本	string	最大32字节			
0x0802	硬件版本	RO	否	硬件版本	string	最大32字节			
0x0803	产品型号	RO	否	产品型号	string	最大32字节			
0x0804	生产信息	RO	否	生产信息	string	最大32字节			生产日期，生产批次等信息
0x0805	厂家信息	RO	否		string	最大32字节			生产商名称信息

-----------------------------------------------------------------------------------------*/

/////-----设备类型----
//#define   DEV_AC_METER_TYPE     (0xD006)     //烟雾传感器（D006H） 

/////------三相电表-----------------
////以下枚举的取值 需要结合 数组 ac_meter_iot_id_table[ac_meter_index_end]的下标进行关联的定义---同步更新----
// typedef enum{
//     ac_meter_index_run_data_begin      = 0,    // 设备运行参数
//     ac_meter_index_config_data_begin   = 35,   // 设备配置参数-------------超过 8个时需要分帧处理
//     ac_meter_index_factory_info_begin  = 39,   // 设备出厂信息
//     ac_meter_index_end = 43,
// } ac_meter_attr_index_e;




////物模型更新到 <<交流电表物模型-v1.4>>
/////------三相电表-----------------
////以下枚举的取值 需要结合 数组 ac_meter_iot_id_table[ac_meter_index_end]的下标进行关联的定义---同步更新----
typedef enum{
    ac_meter_index_run_data_begin      = 0,   // 设备运行参数
    ac_meter_index_run_data_end        = 20,
    ac_meter_index_config_data_begin   = 18,   // 设备配置参数-------------超过 8个时需要分帧处理
    ac_meter_index_factory_info_begin  = 34,  // 设备出厂信息
    ac_meter_index_end = ac_meter_index_factory_info_begin + 5,
} ac_meter_attr_index_e;



#pragma pack(1)

  ///-------交流电表物模型-v1.2--------------
//0x0421	1057	报警信息"0=正常，1=告警
typedef union{
    struct{
                       // 0=无警报，1=发生警报
      uint32_t HU :  1;// bit0=“ H- U” （ 过线电压）
      uint32_t LU :  1;// bit1=“ L- U” （ 欠线电压）
      uint32_t HF :  1;// bit2=“ H- F” （ 过频率）
      uint32_t LF :  1;// bit3=“ L- F” （ 欠频率）
      uint32_t HP :  1;// bit4=“ H- P” （ 过功率）
      uint32_t LP :  1;// bit5=“ L- P” （ 欠功率）
      uint32_t HI :  1;// bit6=“ H- I” （ 过电流）

      // uint32_t LPF :  1;// bit7=“ L- PF” （ 欠功率因数）
      // uint32_t HBU :  1;// bit8=“ H- b.U”（ 电压不平衡）
      // uint32_t HBI:  1;// bit9=“ H- b.I” （ 电流不平衡）


 ///-------交流电表物模型-v1.4--------------
      uint32_t LPF :  1;// bit7=“ L- PF” （ 保留
      uint32_t HBU :  1;// bit8=“ H- b.U”（ 保留
      uint32_t HBI:   1;// bit9=“ H- b.I” （ 保留

      uint32_t LI:    1; //bit10：（欠电流）

      uint32_t res1:21; //其余：保留

    }bit;
    uint32_t value;
} ac_meter_alarm_u;


//烟雾传感器（D006H）的数据
///---------RO 数据--------//
typedef struct{

    ///-------交流电表物模型-v1.2--------------
    ac_meter_alarm_u  alarm;   //0x0421	1057	报警信息"0=正常，1=告警 rptAlm
    ///-------交流电表物模型-v1.2--------------
       
       
    //uint16_t  T;            //温度---0.1摄氏度 temperature  
    uint16_t  Freq	;       //电压频率	0.1Hz
    uint16_t  Ileak ;    //0x0407	,//漏电电流	RO	否	运行参数	uint16_t		1m  
    uint16_t  PF_A	  ;     //A相功率因素0.01  uint8_t	0~100
    uint16_t  PF_B	  ;     //B相功率因素0.01
    uint16_t  PF_C	  ;     //C相功率因素0.01
    uint16_t  PF_T	  ;     //总功率因素0.01  uint8_t	0~1  
    uint16_t  Urms_A ;      //A相电压---0.1V
    uint16_t  Urms_B ;      //B相电压---0.1V	
    uint16_t  Urms_C ;      //C相电压---0.  
    uint32_t  Uab ;// 0x0415	,//线电压UAB	RO	否	运行参数	uint16_t		0.1V
    uint32_t  Ubc ;// 0x0416	,//线电压UBC	RO	否	运行参数	uint16_t		0.1V
    uint32_t  Uca ;// 0x0417	,//线电压UAC	RO	否	运行参数	uint16_t		0.  
    uint32_t  Irms_A;       //A相电流0.01A 
    uint32_t  Irms_B;       //B相电流0.01A- 
    uint32_t  Irms_C;       //C相电流0.01A 

    uint32_t  Power_A	;     //A相有功功率--单位--(1W)	
    uint32_t  Power_B	;     //B相有功功率--单位--(0.001kW)	
    uint32_t  Power_C	;     //C相有功功率--单位--(0.001kW)	
    uint32_t  Power_T	;     //总有功功率---单位--(0.001kW)	  
    uint32_t  Qa	;           //A相无功功率	--0.001kvar
    uint32_t  Qb	;           //B相无功功率	var
    uint32_t  Qc	;           //C相无功功率	var
    uint32_t  Qt	;           //总无功功率	var

    uint32_t  Sa ;// 0x041C	,//A相视在功率	RO	否	运行参数	uint16_t		VA
    uint32_t  Sb ;// 0x041D	,//B相视在功率	RO	否	运行参数	uint16_t		VA
    uint32_t  Sc ;// 0x041E	,//C相视在功率	RO	否	运行参数	uint16_t		VA
    uint32_t  St ;// 0x041F	,//总视在功率	RO	否	运行参数	uint16_t		VA		

    uint32_t  EnergyP_A ;// 0x0412	,//A相有功电能	R/W	否	运行参数	uint32_t		0.01kWh
    uint32_t  EnergyP_B ;// 0x0413	,//B相有功电能	R/W	否	运行参数	uint32_t		0.01kWh
    uint32_t  EnergyP_C ;// 0x0414	,//C相有功电能	R/W	否	运行参数	uint32_t		0.01kWh

    uint32_t  EnergyP_T ;// 0x040B	,//总有功电能	R/W	否	运行参数	uint32_t		0.01kWh
    uint32_t  EnergyQ_T ;// 0x0420	,//总无功电能	R/W	否	运行参数	uint32_t		0.01kWh  
    //uint32_t  EnergyS_T ;// 0x0420	,//视在电能量	R/W	否	运行参数	uint32_t		0.01kWh  

    uint32_t  DI ;// 0x0421	,//DI	RO		运行参数	uint16_t		
    uint32_t  DO ;// 0x0422	,//DO	R/W		运行参数	uint16_t		

} ac_meter_data_t;

///---------R/W	配置参数--------//
typedef struct{

    uint8_t   addr;              //设备地址  0~255			
    uint8_t   channel;           //0x0602	设备通道编号	0~31	

    uint16_t		CT	;     //电流互感器变比		R/W		配置信息	uint16_t		
    uint16_t		PT	;     //电压互感器变比		R/W		配置信息	uint16_t	

    uint16_t  do1 ;    //通道1数字输出--	R/W	否	运行参数	
    uint16_t  do2 ;    //通道2数字输出--	R/W	否	运行参数	

    ///-------交流电表物模型-v1.2--------------
    //// EP_pulse参数-目前不需要配置--
    uint16_t	pulseConst 	;   // 0x0440	1088		电能脉冲常数  EP_pulse	pulseConst R/W	否	配置信息	uint16_t	16-1600	100imp/kWh		默认值100 pulse
    ac_meter_alarm_u  almEn ; //0x0441	1089		报警使能 alarm_enable	R/W	 "0=不启用，1=启用  almEn


 ///-------交流电表物模型-v1.4--------------
    uint16_t  thVoltHi	; //电压高阈值	R/W	否	配置信息	uint16_t		0.1V	0x0470	1136
    uint16_t  thVoltLow	; //电压低阈值	R/W	否	配置信息	uint16_t		0.1V	0x0471	1137
    uint16_t  thFreqHi	; //过频率阈值	R/W	否	配置信息	uint16_t		0.1Hz	0x0472	1138
    uint16_t  thFreqLow	; //欠频率阈值	R/W	否	配置信息	uint16_t		0.1Hz	0x0473	1139
    uint16_t  thPowerHi	; //过功率阈值	R/W	否	配置信息	uint16_t		1W	0x0474	1140
    uint16_t  thPowerLow; //欠功率阈值	R/W	否	配置信息	uint16_t		1W	0x0475	1141
    uint16_t  thCurrHi	; //过电流阈值	R/W	否	配置信息	uint16_t		0.01A	0x0476	1142
    uint16_t  thCurrLow	; //欠电流阈值	R/W	否	配置信息	uint16_t		0.01A	0x0477	1143


} ac_meter_config_t;




//---配置节点信息----------


typedef union
{
	struct{		
	  uint16_t online                  :1;	  //节点在线标志-----1:在线，0:离线，
	  uint16_t res                     :15;  
		
	}bit;
	
  uint16_t all;

} ac_meter_node_flags_u;



// typedef enum{
//     index_1,    //
//     index_max,
// } ac_meter_index_e;


//#define   AC_METER_NODE_NUM_MAX    (255U)  
#define   AC_METER_NODE_NUM_MAX    (NODE_NUM_MAX)     ///与配置节点总数相关-----



typedef struct{
    ac_meter_data_t      data;
    ac_meter_config_t    config;
    dev_attr_com_info_t  info;
    ac_meter_node_flags_u  flags;
   // char   dev_name_str[32];   
} ac_meter_attr_t;


#pragma pack()


extern const  uint16_t  ac_meter_iot_id_table[ac_meter_index_end];

/**
 * @brief 
 * 
 */
void ac_meter_param_init(void);

uint8_t ac_meter_add_obj(uint8_t idx,uint8_t channel,uint8_t addr);


/**
 * @brief 
 * 
 * @param in idx 
 * @return ac_meter_attr_t ==NULL 查询出错,   !NULL 查询ok
 */
ac_meter_attr_t*  ac_meter_get_obj(uint8_t idx) ;  //ac_meter_attr_get


/**
 * @brief ac_meter_set_obj() 赋值节点数据
 * 
 * @param in idx
 * @param in p_attr
 * @return 0=失败，1=成功 
 */
uint8_t  ac_meter_set_obj(uint8_t idx,ac_meter_attr_t* p_attr )  ;



  //  ac_meter_index_run_data_begin      = 0,    // 设备运行参数
  //  ac_meter_index_config_data_begin   = 3,    // 设备配置参数
/**
 * @brief 根据下标，获取iot_id 的编号
 * 
 * @param idx 
 */
uint16_t  ac_meter_get_iot_id(uint8_t idx);


/**
 * @brief 
 * 
 * @param attr_id 
 * @param attr_out 
 */
void ac_meter_get_attr(uint8_t idx,uint16_t attr_id, IotModel* attr_out);


/**
 * @brief 
 * 
 * @param attr_in 
 * @return uint8_t 
 */
uint8_t ac_meter_set_attr(uint8_t idx,IotModel* attr_in);


void ac_meter_test_tick_min(uint8_t idx);

////刷新节点运行数据
void ac_meter_update_tick_min(uint8_t idx,int8_t temper ,uint32_t ac_meter );

void ac_meter_obj_update(uint8_t idx,ac_meter_attr_t* pliq_in  );




#endif //------__DEV_AC_METER_H__




















